home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / C / LIB / HASWIN / HASWIN3 / _files / _haswin / TOFILER._c < prev    next >
Text File  |  1991-05-27  |  8KB  |  202 lines

  1. /* > $.CLIB.C.tofiler
  2.  *
  3.  *      HASWIN Graphics Library
  4.  *     =========================
  5.  *
  6.  *      Copyright (C) H.A.Shaw 1990.
  7.  *              Howard A. Shaw.
  8.  *              The Unit for Space Sciences,
  9.  *              Room 165,
  10.  *              Physics Building,
  11.  *              University of Kent at Canterbury.
  12.  *              Canterbury.
  13.  *              Kent.  CT2 7NJ
  14.  *      You may use and distribute this code freely, however please leave
  15.  *      it alone.  If you find bugs (and there will be many) please contact
  16.  *      me and the master source can be modified.  If you keep me informed
  17.  *      of who you give copies of this to then I can get release upgrades
  18.  *      to them.
  19.  *
  20.  *      These routines communicate with the Filer.  This was supposed to be
  21.  *      a file full of inter-process routines, but it never came off.  I
  22.  *      hope to get it all done properly in the next release.
  23.  *
  24.  *      So far these routines are only to support the thing Acorn wants
  25.  *      about closing a window with the adjust button.  It might seem like
  26.  *      a good idea, but the code to do it with the proper defaults is a
  27.  *      pain in the bum.  Luckily we don't often have to do this.
  28.  */
  29. #include "includes.h"
  30. /*
  31.  *      this piece of code sends the message "msg" to the filer giving the
  32.  *      full file spec of the file "fname" in the filesystem "fsys".
  33.  *      "msg" must either be MESSAGE_FilerOpenDir or MESSAGE_FilerCloseDir.
  34.  *      It has to create a full filespec, which is not easy!!
  35.  */
  36. static int haswin_send_to_filer(char *fs, char *fname, int msg) {
  37.  
  38.         register char   *ptr;
  39.         register int    i;
  40.         char            dname[64], cdir[64], fsys[64], *realname;
  41.         int             fsysnum, oldfsysnum;
  42.         buffer          pbuf;
  43.         _kernel_swi_regs        regs;
  44.  
  45.         if ((msg != MESSAGE_FilerCloseDir) && (msg != MESSAGE_FilerOpenDir))
  46.                 return(HASWIN_FALSE);
  47. /*
  48.  *      find the filesystem number.  Scan fname looking for ":"
  49.  *      NB:   If ':' is first character then use default filesystem and
  50.  *            take ':' as discname starter.
  51.  *            Can't have ':' except as filesystem separator.
  52.  */
  53.         realname = fname;
  54.         strcpy(fsys, fs);
  55.         for (ptr=fname+1; *ptr; ptr++) {
  56.                 if (*ptr == ':') {
  57.                         *ptr = '\0';
  58.                         strcpy(fsys, fname);
  59.                         *ptr = ':';
  60.                         fname = ptr+1;
  61.                         break;
  62.                 } else if ((*ptr == '.') || (*ptr == '$'))
  63.                         break;
  64.         }
  65.         regs.r[0] = 13;
  66.         regs.r[1] = (int)fsys;
  67.         regs.r[2] = 0;
  68.         if ((!haswin_swi(OS_FSControl, ®s)) || (!regs.r[2])) {
  69.                 haswin_interrorprintf("filing system %s not available, cannot access filer for file %s", fsys, realname);
  70.                 return(HASWIN_FALSE);
  71.         }
  72.         fsysnum = regs.r[1];
  73.  
  74. /*
  75.  *      change the current filesystem, so we can do proper discname
  76.  *      and current directory lookups.  This is a *REAL* pain!!
  77.  *
  78.  *      1)    use OS_FSControl to set temp fsys to current fsys
  79.  *      2)    use OS_Args to read temp fsys number
  80.  *      3)    use OS_FSControl to change current and temp fsys
  81.  *      4)    ... do processing ...
  82.  *      5)    remember to use OS_FSControl to reset current and temp fsys
  83.  *      6)    leave routine.
  84.  */
  85.         regs.r[0] = 19;
  86.         haswin_swi(OS_FSControl, ®s);
  87.         regs.r[0] = 0;
  88.         regs.r[1] = 0;
  89.         haswin_swi(OS_Args, ®s);
  90.         oldfsysnum = regs.r[0];
  91.         regs.r[0] = 14;
  92.         regs.r[1] = (int)fsys;
  93.         haswin_swi(OS_FSControl, ®s);
  94. /*
  95.  *      now find the disc name.  It starts ":" and ends ".".
  96.  *      If we don't find a name, do a OS_GBPB number 5 to get it.
  97.  */
  98.         if (*fname == ':') {
  99.                 fname++;
  100.                 for (ptr=fname; *ptr; ptr++) {
  101.                         if (*ptr == '.') {
  102.                                 *ptr = '\0';
  103.                                 strcpy(dname, fname);
  104.                                 *ptr = '.';
  105.                                 fname = ptr+1;
  106.                                 break;
  107.                         }
  108.                 }
  109.         } else {
  110.                 regs.r[0] = 5;
  111.                 regs.r[2] = (int)&pbuf;
  112.                 haswin_swi(OS_GBPB, ®s);
  113.                 pbuf.c[pbuf.c[0]+1] = '\0';
  114.                 if (!strncmp(&pbuf.c[1], "\"Unset\"", 7))
  115.                         strcpy(dname, "");
  116.                 else
  117.                         strcpy(dname, &pbuf.c[1]);
  118.         }
  119.         if (*fname != '$') {
  120.                 /* file must be relative to current dir */
  121.                 regs.r[0] = 6;
  122.                 regs.r[2] = (int)&pbuf;
  123.                 haswin_swi(OS_GBPB, ®s);
  124.                 pbuf.c[pbuf.c[1]+2] = '\0';
  125.                 strcpy(cdir, &pbuf.c[2]);
  126.         } else
  127.                 *cdir = '\0';
  128. /*
  129.  *      create the message buffer
  130.  */
  131.         pbuf.i[3] = 0;
  132.         pbuf.i[4] = msg;
  133.         pbuf.i[5] = fsysnum;
  134.         pbuf.i[6] = 0;
  135.         if (*cdir) {
  136.                 if (!strncmp(cdir, "\"Unset\"", 7))
  137.                          strcpy(cdir, "$");
  138.                 sprintf(&pbuf.c[28], "%s::%s.%s.%s",fsys,dname,cdir,fname);
  139.         } else
  140.                 sprintf(&pbuf.c[28], "%s::%s.%s",   fsys,dname,     fname);
  141. /*
  142.  *      now look at the filename given, and remove leaves until
  143.  *      we get a real directory.
  144.  */
  145.         do {
  146.                 regs.r[0] = 17;
  147.                 regs.r[1] = (int)(&pbuf.c[28]);
  148.                 haswin_swi(OS_File, ®s);
  149.                 switch (regs.r[0]) {
  150.                 case 2:
  151.                         /* a directory, what we want */
  152.                         break;
  153.                 case 1:
  154.                         /* a file, so scan back leafname and remove */
  155.                         i = asciilen(&pbuf.c[28]);
  156.                         while ((i>0) && (pbuf.c[28+i] != '.'))
  157.                                 i--;
  158.                         if (i == 0) {
  159.                                 haswin_interrorprintf("cannot find file %s (%s) in filesystem %s", realname, &pbuf.c[28], fsys);
  160.                                 regs.r[0] = 14;
  161.                                 regs.r[1] = oldfsysnum;
  162.                                 haswin_swi(OS_FSControl, ®s);
  163.                                 return(HASWIN_FALSE);
  164.                         }
  165.                         pbuf.c[28+i] = '\0';
  166.                         break;
  167.                 default:
  168.                         /* does not exist, so error */
  169.                         haswin_interrorprintf("file %s (%s) does not exist in filesystem %s", realname, &pbuf.c[28], fsys);
  170.                         regs.r[0] = 14;
  171.                         regs.r[1] = oldfsysnum;
  172.                         haswin_swi(OS_FSControl, ®s);
  173.                         return(HASWIN_FALSE);
  174.                 }
  175.         } while (regs.r[0] != 2);
  176.         pbuf.i[0] = 28 + ((asciilen(&pbuf.c[28])+4) & 0xFFFFFFFC);
  177. /*
  178.  *      finally send the message to the filer
  179.  */
  180.         regs.r[0] = 17;
  181.         regs.r[1] = (int)&pbuf;
  182.         regs.r[2] = 0;
  183.         haswin_swi(HASWIN_Send_message, ®s);
  184.         regs.r[0] = 14;
  185.         regs.r[1] = oldfsysnum;
  186.         haswin_swi(OS_FSControl, ®s);
  187.         return(HASWIN_TRUE);
  188. }
  189.  
  190. int haswin_openfiler(char *fsys, char *fname) {
  191.  
  192.         return(haswin_send_to_filer(fsys, fname, MESSAGE_FilerOpenDir));
  193. }
  194.  
  195. int haswin_closefiler(char *fsys, char *fname) {
  196.  
  197.         return(haswin_send_to_filer(fsys, fname, MESSAGE_FilerCloseDir));
  198.  
  199. }
  200.  
  201.  
  202.